﻿using ISS.IR.RRNMS.Library.DataAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace ISS.IR.RRNMS.Library.BusinessLogic
{
    public class QueryBuilder
    {
        public List<EntityField> GetEntityFields(int entityId)
        {
            try
            {
                return Main.GetEntityFields(entityId);
            }
            catch (Exception exc)
            {
                return null;
            }


        }

        public List<Item> GetCategoryItems(int categoryId)
        {
            try
            {
                return Main.GetCategoryItems(categoryId);
            }
            catch (Exception exc)       
            {
                return null;
            }
        }

        public List<Operator> GetAllOperators()
        {
            try
            {
                return Main.GetAllOperators();
            }
            catch (Exception exc)
            {
                return null;
            }
        }

        public DataTable GetSearchResult(string query)
        {
            try
            {
                return Main.ExecuteTextQuery(query);
            }
            catch (Exception exc)
            {
                return null;
            }
        }

        public string GetQueryFromCondition(QueryCondition queryCondition)
        {
            try
            {
                if (queryCondition.LookupValue != 0) // lookup
                    return queryCondition.FieldSchemaName + " " + queryCondition.OperatorSchemaName + " " + queryCondition.LookupValue;
                else if (queryCondition.LookupGuidValue != Guid.Empty) // Main Entity lookup
                    return queryCondition.FieldSchemaName + " " + queryCondition.OperatorSchemaName + " '" + queryCondition.LookupGuidValue.ToString() + "'";

                else if (!queryCondition.OperatorSchemaName.Contains("IR_VALUE"))
                    return queryCondition.FieldSchemaName + " " + queryCondition.OperatorSchemaName + " " + queryCondition.Value;
                else
                {
                    switch (queryCondition.OperatorSchemaName)
                    {
                        case "=IR_VALUE":
                            queryCondition.OperatorSchemaName = " = N'" + queryCondition.Value + "'";
                            break;
                        case "<>IR_VALUE":
                            queryCondition.OperatorSchemaName = " <> N'" + queryCondition.Value + "'";
                            break;

                        case "LIKE IR_VALUE%":
                            queryCondition.OperatorSchemaName = " LIKE N'" + queryCondition.Value + "%'";
                            break;

                        case "LIKE %IR_VALUE":
                            queryCondition.OperatorSchemaName = " LIKE N'%" + queryCondition.Value + "'";
                            break;

                        case "LIKE %IR_VALUE%":
                            queryCondition.OperatorSchemaName = " LIKE N'%" + queryCondition.Value + "%'";
                            break;

                        default:
                            return string.Empty;
                    }
                    return queryCondition.FieldSchemaName + " " + queryCondition.OperatorSchemaName;
                }
            }
            catch (Exception exc)
            {
                return "";
            }
        }

        public Guid CreateView(string viewName,int entityId, List<QueryCondition> conditions)
        {
            return Main.CreateView(viewName,entityId, conditions);
        }

        public bool UpdateView(Guid viewId, string viewName, List<QueryCondition> conditions)
        {
            return Main.UpdateView(viewId,viewName, conditions);
        }

        public bool DeleteView(Guid viewId)
        {
            return Main.DeleteView(viewId);
        }

        public List<QueryCondition> GetQueryConditions(Guid viewId)
        {
            return Main.GetQueryConditions(viewId);
        }

        public List<Item> GetSearchViews(int entityId)
        {
            return Main.GetSearchViews(entityId);
        }
    }
}
